let enumIdList = new Array();

/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['form'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;

    let type = json.type;
    let wechatApps = json.wechatApps;
    let wechatIndex = 0;

    // zmnLayUtil.layFormVerify(form);
    initWechatNode(json.wechats);

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {

        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB']
        });
        let data = obj.field;

        if (type === 2) {
            submitWeChat(data);
        } else if (type === 3 || type === 1) {
            submitSmsAndAli(data);
        } else if (type === 4) {
            submitBaiduTemplate(data);
        }
        layer.close(index);
        return false;
    });

    let weChatPramArr = [];

    function submitWeChat(data) {
        let remainNode = $("#wechats").children("div");
        remainNode.each(function (index, item) {
            let thisNode = $(item);
            let wechat = {templateId: data.templateId};
            let wechatMessageNode = thisNode.find(".wechatMessage");
            wechat.wechatMessage = $(wechatMessageNode).val()
            let wechatEndMessageNode = thisNode.find(".wechatEndMessage");
            wechat.wechatEndMessage = $(wechatEndMessageNode).val()
            let wechatIdNode = thisNode.find(".wechatId");
            wechat.thirdAppId = $(wechatIdNode).val()
            let wechatTempIdNode = thisNode.find(".wechatTempId");

            wechat.wechatTempId = $(wechatTempIdNode).val()
            weChatPramArr.push(wechat);
        });
        if (zmnUtil.isNull(weChatPramArr) || weChatPramArr.length <= 0) {
            layer.msg("请配置微信相关设置在提交!", {icon: 2});
            return;
        }
        let jsonPram = JSON.stringify(weChatPramArr);
        $.ajax({
            async: false,
            type: "POST",
            url: "/msg/temp/editWeChatContent.action",
            contentType: "application/json", dataType: "json", data: jsonPram,
            success: function (data) {
                if (data.status === 200) {
                    layer.msg("操作成功！", {icon: 1});
                } else {
                    layer.msg(data.message, {icon: 2});
                }
            }
        });
        return false;
    }

    function submitSmsAndAli(data) {
        if (data.way === 1) {
            let messageAccount = $('#messageAccount').val();
            let messagePwd = $('#messagePwd').val();
            if (zmnUtil.isEmptyNull(messageAccount) || zmnUtil.isEmptyNull(messagePwd)) {
                layer.msg("通道账号密码不能为空!");
                return false;
            }
        }
        let uri;
        if (type === 1) {
            uri = "/msg/temp/editSmsContent.action";
        } else {
            uri = "/msg/temp/editZfbContent.action";
        }
        $.ajax({
            async: false,
            type: "POST",
            url: uri,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (data) {
                let message = data.status == 200 ? "操作成功！" : data.message;
                layer.msg(message);
            },
            error: function (XMLHttpRequest, status, errorThrown) {
            }
        });
        return false;
    }

    function submitBaiduTemplate(data) {
        $.ajax({
            async: false,
            type: "POST",
            url: "/msg/temp/editBaiduContent.action",
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (data) {
                if (data.status == 200) {
                    layer.msg("操作成功", {icon: 1});
                } else {
                    layer.msg(data.message, {icon: 2});
                }
                return false;
            },
            error: function (XMLHttpRequest, status, errorThrown) {
            }
        });
        return false;
    }

    form.on('select(way)', function (obj) {
        if (obj.value == 1) {
            $('#showType').show();
        } else {
            $('#showType').hide();
        }
    });

    function initWechatNode(wechatData) {
        if (zmnUtil.isEmptyNull(wechatData)) {
            let baseWechatHtml = getWechatConfigBaseHtml(1, {
                wechatMessage: "",
                wechatEndMessage: "",
                wechatId: "",
                wechatTempId: ""
            });
            $("#wechats").append(baseWechatHtml);
            wechatIndex = 1;
            initWeChatAppSelect(null, wechatIndex);
        } else {
            for (let i = 0; i < wechatData.length; i++) {
                wechatIndex++;
                let baseWechatHtml = getWechatConfigBaseHtml(wechatIndex, wechatData[i]);
                $("#wechats").append(baseWechatHtml);
                initWeChatAppSelect(wechatData[i].wechatId, wechatIndex);
            }
        }
        form.render();
    }

    function initWeChatAppSelect(selectVale, num) {
        let tempCompanyHtml = `<option value="">请选择微信公众号</option>`;
        if (!zmnUtil.isEmptyNull(wechatApps)) {
            for (let i = 0; i < wechatApps.length; i++) {
                let exAttr = "";
                let exText = "";
                let item = wechatApps[i];
                if (selectVale === item.thirdAppId) {
                    tempCompanyHtml += '<option selected value="' + item.thirdAppId + '" ' + exAttr + '>' + item.name + " " + exText + '</option>';
                } else {
                    tempCompanyHtml += '<option value="' + item.thirdAppId + '" ' + exAttr + '>' + item.name + " " + exText + '</option>';
                }
            }
        }
        let wechatIdClass = "wechatIdClass" + num
        $("." + wechatIdClass).html(tempCompanyHtml);
    }

    $(document).on('click', '#addWechatNode', function () {
        addWechatNode();
    });
    $(document).on('click', '#deleteWechatNode', function () {
        deleteWechatNode(this);
    });

    function addWechatNode() {
        let addIndex = wechatIndex + 1;
        let baseWechatHtml = getWechatConfigBaseHtml(addIndex, {
            wechatMessage: "",
            wechatEndMessage: "",
            wechatId: "",
            wechatTempId: ""
        });
        let idName = getWechatNodeIdName(wechatIndex);
        $("#" + idName).after(baseWechatHtml);
        wechatIndex++;
        initWeChatAppSelect(null, wechatIndex);
        form.render();
    }

    function deleteWechatNode(node) {
        let parentNode = $(node).parent().parent();
        parentNode.remove();
        wechatIndex--;

        let remainNode = $("#wechats").children("div");
        remainNode.each(function (index, item) {
            let thisNode = $(item);
            let num = index + 1;
            let nodeIdName = getWechatNodeIdName(num);
            thisNode.attr('id', nodeIdName);
            thisNode.attr('data-value', num);
            let titleName = thisNode.find(".titleName");
            titleName.html("微信" + num + "&nbsp;&nbsp;");
        });
    }

    function getWechatConfigBaseHtml(num, item) {

        let addButton = "<button type=\"button\" id='deleteWechatNode' class=\"layui-btn layui-btn-danger layui-btn-xs\">-删除</button>";
        if (num === 1) {
            addButton = "<button type=\"button\" id='addWechatNode' class=\"layui-btn layui-btn layui-btn-xs\">+添加</button>";
        }
        let nodeIdName = getWechatNodeIdName(num);

        let configHtml = "<div id='" + nodeIdName + "' data-value='" + num + "'><div class=\"layui-card-header layui-margin-b\"><b class='titleName'>微信" + num + "&nbsp;&nbsp;</b>" +
            "" + addButton + "\n</div>" +
            "                        <div class=\"layui-form-item\">\n" +
            "                            <label class=\"layui-form-label layui-form-required\">正文：</label>\n" +
            "                            <div class=\"layui-input-inline\" style=\"width:590px\">\n" +
            "                                <textarea name=\"wechatMessage\" class=\"layui-textarea wechatMessage\" placeholder=\"填写正文\" lay-verify=\"required\" lay-verType=\"tips\" maxlength=\"255\">" + item.wechatMessage + "</textarea>\n" +
            "                            </div>\n" +
            "                        </div>\n" +
            "\n" +
            "                        <div class=\"layui-form-item\">\n" +
            "                            <label class=\"layui-form-label\">结束语：</label>\n" +
            "                            <div class=\"layui-input-inline\" style=\"width:590px\">\n" +
            "                                <textarea name=\"wechatEndMessage\" class=\"layui-textarea wechatEndMessage\" placeholder=\"填写结束语\" lay-verify=\"\" lay-verType=\"tips\" maxlength=\"50\">" + item.wechatEndMessage + "</textarea>\n" +
            "                            </div>\n" +
            "                        </div>\n" +
            "\n" +
            "                        <div class=\"layui-form-item\">\n" +
            "                            <label class=\"layui-form-label layui-form-required\">微信公众号ID：</label>\n" +
            "                            <div class=\"layui-input-inline\"  style=\"width: 300px\">\n" +
            "                                <select class='wechatId wechatIdClass" + num + "' name=\"wechatId\"></select>\n" +
            "                            </div>\n" +
            "                        </div>\n" +
            "                        <div class=\"layui-form-item\">\n" +
            "                            <label class=\"layui-form-label layui-form-required\">微信模板ID：</label>\n" +
            "                            <div class=\"layui-input-inline\" style=\"width: 300px\">\n" +
            "                                <input type=\"text\" name=\"wechatTempId\" placeholder=\"调用对应的微信模板ID\" lay-verify=\"required\"\n" +
            "                                       lay-verType=\"tips\" value=\"" + item.wechatTempId + "\"\n" +
            "                                       autocomplete=\"off\" class=\"layui-input wechatTempId\" maxlength=\"50\"/>\n" +
            "                            </div>\n" +
            "                        </div></div>";
        return configHtml;
    }

    function getWechatNodeIdName(index) {
        return "weChatNode" + index;
    }

    //对外暴露的接口
    exports('msg_template_content', {});
});

function wordStatic(input, emeId) {
    let content = document.getElementById(emeId);
    if (content && input) {
        let value = input.value.replace(/\\n|\\r/gi, '');
        content.innerText = value.length;
    }
}